package com.jksoft.darewayyyy.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.jksoft.darewayyyy.global.MyApplication;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * Created by Administrator on 2015/9/18.
 */
public class FileUtils {
    private Context mContext;
    public FileUtils(Context context) {
        this.mContext = context;
    }
    public static String getDiskCacheDir(Context context, String folder) {
        String cachePath = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            cachePath = context.getExternalCacheDir().getPath() + "/" + folder;
        } else {
            cachePath = context.getCacheDir().getPath() + "/" + folder;
        }
        return cachePath;
    }

    public static File getSaveFile(String fileName) {
        File file = new File(MyApplication.appPath  + File.separator + fileName);
        Log.e("file", "file=" + file.getAbsolutePath());
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public String saveBitmap(Bitmap bitmap, String picName) {
        String saveFilePath = "";
        String filePath = "";
        String SDPATH = "";
        if (!TextUtils.isEmpty(picName) && bitmap != null) {
            boolean ishasSD = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
            if (ishasSD) {
//                SDPATH = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath();
                filePath = MyApplication.appPath + "/";
            }
            File saveFile = new File(filePath, picName);
            if (!saveFile.exists()) {
                saveFile.mkdirs();
            }
            saveFilePath = filePath + picName;
            if (saveFile.exists()) {
                saveFile.delete();
            }
            try {
                FileOutputStream out = new FileOutputStream(saveFile);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                saveFilePath = "";
            } catch (IOException e) {
                e.printStackTrace();
                saveFilePath = "";
            }
            Uri localUri = Uri.fromFile(saveFile);
            Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);
            mContext.sendBroadcast(localIntent);
        }
        return saveFilePath;
    }

    public Bitmap getBitmap(String name) {
        String saveFilePath = "";
        String filePath = "";
        String SDPATH = "";
        Bitmap bitmap = null;
        if (!TextUtils.isEmpty(name)) {
            boolean ishasSD = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
            if (ishasSD) {
//                SDPATH = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath();
                filePath = MyApplication.appPath + "/";
            }
            File saveFile = new File(filePath, name);
            saveFilePath = filePath + name;
            if (!saveFile.exists()) {
                return null;
            }
            try {
//                FileOutputStream out = new FileOutputStream(saveFile);
//                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                FileInputStream fis = new FileInputStream(saveFilePath);
                bitmap = BitmapFactory.decodeStream(fis);

                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                saveFilePath = "";
            } catch (IOException e) {
                e.printStackTrace();
                saveFilePath = "";
            }
            Uri localUri = Uri.fromFile(saveFile);
            Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);
            mContext.sendBroadcast(localIntent);
        }
        return bitmap;
    }
}
